﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace Exact_Extension
{
    public partial class MsgUpdateVATInvoiceNo : DevExpress.XtraEditors.XtraForm
    {
        ISHCMC_V_TaxInvoice itemInvoice;
        ExactDataContext DS;
        int flag;// flag : 0 = Create new one , 1 = Update one
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
        public MsgUpdateVATInvoiceNo(ISHCMC_V_TaxInvoice _itemInvoice,ExactDataContext _DS,int _flag)
        {
            InitializeComponent();
            itemInvoice = _itemInvoice;
            DS = _DS;
            flag = _flag;

            var query = from ListItem in DS.Items
                        select ListItem;
            foreach (Item item in query)
            {
                cbxItemCode.Properties.Items.Add(item.ItemCode);
            }
            this.dateEdit_TaxInvoice.EditValue = DateTime.Now;
            if (this.CheckExistTaxInvoiceTracking(itemInvoice, itemInvoice.ItemCode))
            {
                this.txtTaxInvoiceNo.Text = itemInvoice.VATInvoiceNo;
                this.cbxItemCode.Text = itemInvoice.ItemCode;
                if (itemInvoice.Amount.HasValue)
                    this.txtAmount.Text = itemInvoice.Amount.Value.ToString();
                else
                    this.txtAmount.Text = "0";

                if (itemInvoice.VATAmount.HasValue)
                    this.txtVATAmount.Text = itemInvoice.VATAmountValue.Value.ToString();
                else
                    this.txtVATAmount.Text = "0";
            }
            else
            {
                this.cbxItemCode.Text = itemInvoice.ItemCode;
                if (itemInvoice.Amount.HasValue)
                    this.txtAmount.Text = itemInvoice.Amount.Value.ToString();
                else
                    this.txtAmount.Text = "0";

                if (itemInvoice.VATAmount.HasValue)
                    this.txtVATAmount.Text = itemInvoice.VATAmountValue.Value.ToString();
                else
                    this.txtVATAmount.Text = "0";
            }
           
        }
        private bool CheckExistTaxInvoiceTracking(ISHCMC_V_TaxInvoice itemInv,string item)
        {
            var queryTaxInvTracking = from InvTrack in DS.ISHCMC_TaxInvoiceTrackings
                                      where InvTrack.VATInvoiceNo == item
                                      
                                      select InvTrack;
            if (queryTaxInvTracking.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.CheckExistTaxInvoiceTracking(itemInvoice,itemInvoice.VATInvoiceNo))
                {
                    ISHCMC_TaxInvoiceTracking InvTrack = new ISHCMC_TaxInvoiceTracking
                    {
                        InvoiceNo = itemInvoice.ProformaInvoiceNo,
                        PayerCode = itemInvoice.PayerCode,
                        StudentID = itemInvoice.StudentID,
                        VATInvoiceNo = this.txtTaxInvoiceNo.Text,
                        ItemCode = this.cbxItemCode.Text,
                        Amount = Convert.ToDouble(this.txtAmount.Text),
                        Status =0,
                        TaxInvoiceDate = Convert.ToDateTime(this.dateEdit_TaxInvoice.EditValue),
                        VATAmount = Convert.ToDouble(this.txtVATAmount.Text),

                    };
                    DS.ISHCMC_TaxInvoiceTrackings.InsertOnSubmit(InvTrack);
                  
                }
                else
                {

                    ISHCMC_TaxInvoiceTracking InvTrackUpdate = DS.ISHCMC_TaxInvoiceTrackings.Single(c => c.ID == itemInvoice.InvTrackID);
                    if (InvTrackUpdate.VATInvoiceNo != this.txtTaxInvoiceNo.Text)
                    {
                        InvTrackUpdate.Status = 1;
                        if (this.txtComment.Text != null)
                        {
                            InvTrackUpdate.Comment = this.txtComment.Text;
                        }
                        ISHCMC_TaxInvoiceTracking InvTrack = new ISHCMC_TaxInvoiceTracking
                        {
                            InvoiceNo = itemInvoice.ProformaInvoiceNo,
                            PayerCode = itemInvoice.PayerCode,
                            StudentID = itemInvoice.StudentID,
                            VATInvoiceNo = this.txtTaxInvoiceNo.Text,
                            ItemCode = this.cbxItemCode.Text,
                            Amount = Convert.ToDouble(this.txtAmount.Text),
                            Status =0,
                            TaxInvoiceDate = Convert.ToDateTime(this.dateEdit_TaxInvoice.EditValue),
                            VATAmount = Convert.ToDouble(this.txtVATAmount.Text),

                        };
                        DS.ISHCMC_TaxInvoiceTrackings.InsertOnSubmit(InvTrack);
                    }
                    
                   
                }
                DS.SubmitChanges();
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                this.DialogResult = DialogResult.No;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cbxItemCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbxItemCode.Text == "APPF")
            {
                this.txtAmount.Text = "25000000";
            }
            if (this.cbxItemCode.Text == "REENF")
            {
                this.txtAmount.Text = "21000000";
            }
            if (this.cbxItemCode.Text == "COE")
            {
                this.txtAmount.Text = "322500000";
            }
            if (this.cbxItemCode.Text == "ADF")
            {
                this.txtAmount.Text = "64500000";
            }
            if (this.cbxItemCode.Text == "TF")
            {
                var queryTaxInvTracking = from InvTrack in DS.ISHCMC_TaxInvoiceTrackings
                                          where InvTrack.InvoiceNo == itemInvoice.ProformaInvoiceNo
                                          && InvTrack.PayerCode == itemInvoice.PayerCode
                                          && InvTrack.StudentID == itemInvoice.StudentID
                                          && InvTrack.ItemCode != "TF"
                                          select InvTrack;
                if (queryTaxInvTracking.Count() > 0)
                {
                    double amount = 0;
                    foreach (ISHCMC_TaxInvoiceTracking itemInvTrack in queryTaxInvTracking)
                    {
                        if (itemInvTrack.Amount.HasValue)
                        {
                            amount += itemInvTrack.Amount.Value;
                        }
                    }

                    amount = itemInvoice.PaidAmount.Value - amount;
                    this.txtAmount.Text = amount.ToString();
                }
                else
                {
                    this.txtAmount.Text = itemInvoice.PaidAmount.Value.ToString();
                }
            }
        }

    
    }
}